<template>
    <div style="margin-left: 20px;margin-right: 20px;margin-top: 10px;">
        <div style="font-size: 16px">语音识别</div>
        <el-upload
                ref="upload"
                multiple
                action=""
                accept=".pcm,.wav"
                :http-request="httpRequest"
                :auto-upload="false">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="onSubmit">提交识别</el-button>
        </el-upload>

        <textarea style="margin-top: 10px;width: 300px;height: 200px;" v-model="form.iatContent"></textarea>
    </div>
</template>

<script>


    export default {
        name: "Recognie",
        data() {
            return {
                form: {
                    fileList: [],
                    iatContent: ''
                }
            }
        },
        methods: {
            onSubmit: function () {
                const vm = this;
                console.log('submit:', this.form);
                this.$refs.upload.submit();
                let formData = new FormData();
                this.form.fileList.forEach(function (file) {// 因为要上传多个文件，所以需要遍历
                    formData.append('file', file, file.name);
                })
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                this.$axios.post("/upload", formData)
                    .then(function (response) {
                        console.log(response);
                        vm.form.iatContent = response;
                        loading.close()
                    })
                    .catch(function (error) {
                        console.log(error);
                    })
            },
            httpRequest(param) {
                // 一般情况下是在这里创建FormData对象，但我们需要上传多个文件，为避免发送多次请求，
                // 因此在这里只进行文件的获取，param可以拿到文件上传的所有信息
                this.form.fileList.push(param.file);
            }
        }
    }
</script>

<style scoped>

</style>